package hotgammon.variants.movestrategy;

import hotgammon.common.Color;
import hotgammon.common.Game;
import hotgammon.common.Location;
import hotgammon.common.MoveStrategy;

/**
 * Represents the BetaMon moving rule
 * @author khanh.nhan
 *
 */
public class BetaMoveStrategy implements MoveStrategy {
	/** AlphaMon implementation to avoid code duplication */
	private AlphaMoveStrategy alpha;
	
	public BetaMoveStrategy(){
		this.alpha = new AlphaMoveStrategy();
	}

	@Override
	public boolean isMoveValid(Location from, Location to, Game game) {
		Color turn = game.getPlayerInTurn();
		Color fromColor = game.getColor(from);
		Color toColor = game.getColor(to);
		
		// moves on the board are not allowed when a player has checkers left in his/her bar
		Location bar = (turn == Color.BLACK ? Location.B_BAR : Location.R_BAR);
		//if a player have checkers in his bar and he doesn't move it out --> illegal
		if (game.getCount(bar) > 0 && from != bar)
			return false;
		
		if (!alpha.isMoveValid(from, to, game)) {
			// blot condition checking because the blot will fail the alpha move strategy test
			if (toColor != Color.NONE && toColor != fromColor && game.getCount(to) == 1)
				return true;
			else
				return false;
				
		}
		return true;
	}
}
